<?php
namespace app\admin\controller;

use app\admin\model\AgentRole as AgentRoleModel;
use app\admin\model\StoreMenu as StoreMenuModel;
use app\common\controller\AdminBaseController;

/**
 * 权限组
 * Class AuthGroup
 * @package app\admin\controller
 */
class AgentRoleController extends AdminBaseController {
    protected $agent_role_model;
    protected $store_menu_model;

    protected function _initialize() {
        parent::_initialize();
        $this->agent_role_model = new AgentRoleModel();
        $this->store_menu_model  = new StoreMenuModel();
    }

    /**
     * 权限组
     * @return mixed
     */
    public function index() {
        $agent_role_list = $this->agent_role_model->paginate(15);
        return $this->fetch('index', ['auth_group_list' => $agent_role_list]);
    }

    /**
     * 添加权限组
     */
    public function add() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->agent_role_model->save($data) !== false) {
                $this->success('保存成功', url('agnet_role/index'));
            } else {
                $this->error('添加失败');
            }
        }else{
        	return $this->fetch();
        }
    }

    /**
     * 更新权限组
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if($id == 1 && $data['status'] != 1){
                $this->error('普通商家不可禁用');
            }
            if ($this->agent_role_model->save($data, $id) !== false) {
                $this->success('更新成功',url('agnet_role/index'));
            } else {
                $this->error('更新失败');
            }
        }else{
        	$auth_group = $this->agent_role_model->find($id);
        	return $this->fetch('update', ['auth_group' => $auth_group]);
        }
    }

    /**
     * 删除权限组
     * @param $id
     */
    public function delete($id) {
        if($id == 1){
            $this->error('普通商家不可删除');
        }
        if ($this->agent_role_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id, $auth_rule_ids = '') {
    	if ($this->request->isPost()) {
            if ($id) {
                $group_data['id']    = $id;
                $group_data['rules'] = is_array($auth_rule_ids) ? implode(',', $auth_rule_ids) : '';
                if ($this->agent_role_model->save($group_data, $id) !== false) {
                    $this->success('授权成功', url('index'));
                } else {
                    $this->error('授权失败');
                }
            }
        }else{
        	return $this->fetch('auth', ['id' => $id]);
        }
    }

    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     */
    public function getJson($id) {
        $auth_group_data = $this->agent_role_model->find($id)->toArray();
        $auth_rules      = explode(',', $auth_group_data['rules']);
        $auth_rule_list  = $this->store_menu_model->field('id,pid,title')->select();

        foreach ($auth_rule_list as $key => $value) {
            in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
        }

        return $auth_rule_list;
    }

}
